﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using FileManager;
using System.IO;
using System.Drawing;
using FileManager.Toolkit;
namespace DalatCooking.Core.Web.HttpHandlers
{
    public class ImageUploadHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string _uploadFolder = "";
            if (context.Request["folder"] != null)
            {
                _uploadFolder = context.Request["folder"];
            }
            else
            {
               _uploadFolder = Setting.String("FILE_IMAGEFOLDER");
            }
            string _imageResizedPath = Setting.String("FILE_IMAGEFOLDER");
            string errMess = "";
            string path = "";
           
            UploadHandler upload = new UploadHandler();

            upload.AllowedExtensions = Setting.String("FILE_ALLOW_IMAGEEXT");
            upload.GenerateUniqueFileName = Setting.Boolean("FILE_ALLOW_UNIQUENAME");

            upload.VirtualSavePath = _uploadFolder;
            upload.FileContentLenght = Setting.Int("FILE_MAXSIZE");
            upload.OverwriteExistingFile = Setting.Boolean("FILE_ALLOW_OVERWRITE");
            upload.GenerateDateFolder = Setting.Boolean("FILE_ALLOW_DATEFOLDER");
            upload.AddTextOnImage = Setting.Boolean("IMAGE_ALLOW_ADDTEXT");
            upload.ResizeImage = Setting.Boolean("IMAGE_ALLOW_RESIZE");

            int imageWidth = Setting.Int("IMAGE_WIDTH");
            int imageHeight = Setting.Int("IMAGE_HEIGHT"); 

            HttpPostedFile postedFile = context.Request.Files["myfile"];
            if (postedFile.FileName.EndsWith(".swf"))
                upload.VirtualSavePath = Setting.String("FILE_FLASHFOLDER");
            // Upload Images
            if (upload.UploadFile(postedFile, ref errMess, ref path) == true)
            {
                if (!path.Contains(".swf"))
                {
                    if (upload.ResizeImage)
                    {
                        // Crop Image
                        string fileToResize = context.Server.MapPath(path);
                        Imaging.ResizeImage(fileToResize, imageWidth, imageHeight);
                        // Imaging.ResizeImage(fileToResize, fileToResize, 105, System.Drawing.Imaging.ImageFormat.Png);
                        
                    }
                    if (upload.AddTextOnImage)
                    {
                        string fileNameIn = context.Server.MapPath(path);
                        string fileNameOut = context.Server.MapPath(Path.GetDirectoryName(path) + "\\" + Guid.NewGuid() + Path.GetExtension(path));
                        string textToImage = Setting.String("SITE_NAME");
                        Color myColor = Color.White;
                        Point textLocation = new Point(10, 10);
                        Font f = new Font("Arial", 30f, FontStyle.Regular);
                        Imaging.AddTextToImage(fileNameIn, fileNameOut, f, myColor, textLocation, textToImage);

                        File.Delete(fileNameIn);
                        string fileNameOutput = Path.GetDirectoryName(path) + "\\" + Path.GetFileName(fileNameOut);

                        //Uri uriSiteRoot = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority));
                        //var output = string.Format("{0}{1}", "", fileNameOutput.Replace(@"\", "/"));

                        //context.Response.ContentType = "text/plain";
                        //context.Response.Write(output);
                        path = string.Format("{0}{1}", "", fileNameOutput.Replace(@"\", "/"));
                    }
                    else
                    {
                        //Uri uriSiteRoot = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority));
                        //var output = string.Format("{0}{1}", "", path);
                        //context.Response.ContentType = "text/plain";
                        //context.Response.Write(output);
                        path = path;
                    }
                    var output = string.Format("{0}{1}", "", path);
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(output);
                }
                else
                {
                    Uri uriSiteRoot = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority));


                    var output = string.Format("{0}{1}", "", path);
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(output);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
